package com.zx.servicelearn.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v4.os.IResultReceiver;
import android.util.Log;

import com.zx.servicelearn.MainActivity;
import com.zx.servicelearn.aidl.IMyAidlInterface;

/**
 * aidl学习,远程服务
 */
public class MyService4 extends Service {
    String TAG = "MyService4";

    public MyService4() {
    }


    IMyAidlInterface.Stub myBinder = new IMyAidlInterface.Stub() {

        @Override
        public int plus(int a, int b) throws RemoteException {
            //test 这样可以让其他应用打开当前应用的activity
//            startActivity(new Intent(getApplicationContext(), MainActivity.class));
            return a + b;
        }

        @Override
        public int muti(int a, int b) throws RemoteException {
            return a - b;
        }
    };

    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate");//调用startService或bindService时候会执行一次，第二次调用不会执行
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand");//调用startService时候会执行
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind");//调用bindService时候会执行一次，没有onUnbind则再次调用onBind不会执行！
        return myBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e(TAG, "onUnbind");//
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy");//调用stopService会执行
        super.onDestroy();
    }
}
